# -*- coding: utf-8 -*-
# created on 2016/10/12

from mathsolver.functions.base import *
from sympy.abc import x


class ReturnSymbol(BaseFunction):
    def solver(self, *args):
        free_symbols = [s for arg in args for s in arg.free_symbols()]
        self.output.append(BaseSymbolValue(dict([(k, 1) for k in free_symbols])))
        return self


class ReturnValue(BaseFunction):
    def solver(self, *args):
        self.output.append(BaseSymbolValue({sympify(args[0].value[0]): sympify(args[0].value[1])}))
        return self


class ReturnZhiXian(BaseFunction):
    def solver(self, *args):
        free_symbols = [str(s) for arg in args for s in arg.free_symbols() if s != x]
        if free_symbols:
            self.output.append(BaseZhiXian({"name": "", "value": ["8*x-y-1 + %s" % "+".join(free_symbols), "0"]}))
        else:
            self.output.append(BaseZhiXian({"name": "", "value": ["8*x-y-1", "0"]}))
        return self


class ReturnEq(BaseFunction):
    def solver(self, *args):
        free_symbols = [str(s) for arg in args for s in arg.free_symbols() if s != x]
        self.output.append(BaseEq(["+".join(free_symbols), "0"]))
        return self


class ReturnZhiXian2(BaseFunction):
    def solver(self, *args):
        self.output.append(BaseZhiXian({"name": args[0].value, "value": ["8*x-y-1", "0"]}))
        return self


class ReturnFunc(BaseFunction):
    def solver(self, *args):
        return self


class ReturnPoints(BaseFunction):
    def solver(self, *args):
        symbol = args[0].free_symbols().union(args[1].free_symbols()).pop()
        names = args[2].value
        self.output.append(BasePoint({"name": names[0], "value": [symbol, 4]}))
        self.output.append(BasePoint({"name": names[1], "value": [5, symbol]}))
        return self


class ReturnPoint(BaseFunction):
    def solver(self, *args):
        self.output.append(BasePoint({"name": "", "value": [2, 4]}))
        return self


class ReturnXianChang(BaseFunction):
    def solver(self, *args):
        symbols = [str(sym) for sym in args[0].free_symbols().union(args[1].free_symbols()) if
                   str(sym) != "x" and str(sym) != "y"]
        self.output.append(
            BaseSymbolValue({sympify('distance(%s,%s)' % tuple(args[2].value)): sympify("+".join(symbols))}))
        return self


class ReturnDisIneq(BaseFunction):
    def solver(self, *args):
        f1, op, f2 = args[0].sympify()
        distances = get_all_child(f1 - f2, lambda xx: xx.func.__name__ == "distance")
        value = self.search(distances[0])
        for v in value:
            self.output.append(BaseIneq([v, "<", "0"]))
        return self


class ReturnDisEq(BaseFunction):
    def solver(self, *args):
        f1, f2 = args[0].sympify()
        distances = get_all_child(f1 - f2, lambda xx: xx.func.__name__ == "distance")
        value = self.search(distances[0])
        for v in value:
            self.output.append(BaseEq([v, "0"]))
        return self


class ReturnPoly(BaseFunction):
    def solver(self, *args):
        free_symbols = [str(s) for arg in args for s in arg.free_symbols() if s != x]
        if free_symbols:
            poly = "+".join(free_symbols)
        else:
            poly = "1"

        self.output.append(BasePoly(poly))
        return self


class ReturnFakeTuoYuan(BaseFunction):
    def solver(self, *args):
        name = args[0].name
        self.output.append(BaseTuoYuan({"name": name, "value": ["a*x**2 + y**2 ", "1"]}))
        return self


class FakeYuan(BaseFunction):
    def solver(self, *args):
        if isinstance(args[0], BaseVariable):
            name = args[0].value
        else:
            name = args[0].name
        self.output.append(BaseYuan({"name": name, "value": ["a*x**2 + y**2 ", "1"]}))
        return self


class FakeYuan2(BaseFunction):
    def solver(self, *args):
        name = args[0].value
        yuan = self.search(name)
        if len(yuan.free_symbols()) == 4:
            self.output.append(BaseYuan({"name": name, "value": ["a*x**2 + y**2 ", "1"]}))
        else:
            self.output.append(BaseYuan({"name": name, "value": ["x**2 + y**2 ", "1"]}))

        self.output.append(BaseYuan({"name": name, "value": ["a*x**2 + y**2 ", "1"]}))
        return self


class ReturnFakeTuoYuan2(BaseFunction):
    def solver(self, *args):

        name = args[1].value
        tuoyuan = self.search(name)
        if len(tuoyuan.free_symbols()) == 4:
            self.output.append(BaseTuoYuan({"name": name, "value": ["a*x**2 + y**2 ", "1"]}))
        else:
            self.output.append(BaseTuoYuan({"name": name, "value": ["x**2 + y**2 ", "1"]}))

        return self


class ShanXingUpdate(BaseFunction):
    def solver(self, *args):
        value = args[0].sympify()
        text = args[1]
        shanxing = self.known["shanxing"] if "shanxing" in self.known else BaseShanXing()
        if "面积" in text:
            shanxing.S = value
        elif "周长" in text:
            shanxing.C = value
        elif "弧长" in text:
            shanxing.L = value
        elif "半径" in text:
            shanxing.R = value
        elif "圆心角" in text:
            shanxing.A = value
        else:
            raise ValueError("unvalid text")

        self.output.append(shanxing)
        return self


class ShanXingSolve(BaseFunction):
    def solver(self, *args):
        text = args[0]
        shanxing = self.search("shanxing")
        assert shanxing
        values = {"S": 1, "C": 1, "L": 1, "R": 1, "A": 1}
        if "面积" in text:
            value = values["S"]
        elif "周长" in text:
            value = values["C"]
        elif "弧长" in text:
            value = values["L"]
        elif "半径" in text:
            value = values["R"]
        elif "圆心角" in text:
            value = values["A"]
        else:
            raise ValueError("unvalid text")
        self.output.append(BaseNumber(value))

        return self


class FakeZhiXian001(BaseFunction):
    def solver(self, *args):
        self.label.add("直线斜率和倾斜角的转化")
        return self


class FakeZhiXian002(BaseFunction):
    def solver(self, *args):
        self.label.add("求两点距离")
        self.label.add("根据边长判断三角形形状")
        return self


class FakeZhiXian003(BaseFunction):
    def solver(self, *args):
        self.label.add("曲线上点到直线的最短距离")
        return self


class FakeZhiXian004(BaseFunction):
    def solver(self, *args):
        self.label.add("求与点距离最大的直线方程")
        return self


class FakeZhiXian005(BaseFunction):
    def solver(self, *args):
        self.label.add("直线经过象限问题")
        return self


class FakeZhiXian006(BaseFunction):
    def solver(self, *args):
        self.label.add("点在直线两侧")
        free_symbols = [s for arg in args for s in arg.free_symbols()]
        self.output.append(BaseSymbolValue(dict([(k, 1) for k in free_symbols])))
        return self


class FakeYuan001(BaseFunction):
    def solver(self, *args):
        self.label.add("圆上到直线定距离的点的个数")
        return self


class FakeYuan002(BaseFunction):
    def solver(self, *args):
        self.label.add("两圆相切求圆的方程")
        return self


class FakeBuDengShi001(BaseFunction):
    def solver(self, *args):
        self.label.add("根据图形求代数式极值")
        return self


class FakeYuanZhui001(BaseFunction):
    def solver(self, *args):
        self.label.add("根据方程判断曲线形状")
        return self


class FakeYuanZhui002(BaseFunction):
    def solver(self, *args):
        symbols = args[0].free_symbols().difference([sympify("x"), sympify("y")])
        expr = "+".join([str(sym) for sym in symbols])
        self.output.append(BaseNumber(expr))
        return self


if __name__ == '__main__':
    pass
